home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1990: Night of the Living Disc / Night of the Living Disc.2mg / Dev.CD.5 / Tools / DTS.Samples / SC09Lister / File.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-25  |  5.1 KB  |  166 lines  |  [B0] Apple IIgs Source Code (0x000A)

  1. /**********************************************************************
  2. *
  3. * lister file.c -- Version 3.0
  4. *
  5. * Developer Technical Support Apple II Sample Code
  6. *
  7. * Copyright (c)
  8. * Apple Computer, Inc.  1990
  9. * All Rights Reserved.
  10. *
  11. * Written by Eric Soldan.
  12. *
  13. * This file contains the code which handles the file IO for lister.
  14. * The file IO for lister is relatively simple.  All it has to do
  15. * is to open a file and read in lines of characters.  In spite of
  16. * the simplicity of the file IO needs of lister, this code has been
  17. * written in a general way so that multiple files can be opened at
  18. * a time by using this code.  All you need to use this code is a
  19. * file IO structure.  Once you have this set up with all the 
  20. * default behaviors defined, you can start using these routines.
  21. *
  22. **********************************************************************/
  23.  
  24. #include <types.h>
  25. #include <memory.h>
  26. #include <stdfile.h>
  27. #include <misctool.h>
  28.  
  29. #define __file__ 1
  30. #include "lister.h"
  31.  
  32. /**********************************************************************/
  33.  
  34. /* This routine is a simple front-end to SFGetFile2, and has
  35. ** supporting code for setting up info in the file IO structure
  36. ** so the other routines can have fun, too.
  37. */
  38.  
  39. unsigned int    getFileName(file, dx, dy, promptStr, filterProc, typeList)
  40. FileIO          *file;
  41. unsigned int    dx, dy;
  42. unsigned long   filterProc, typeList;
  43. char            *promptStr;
  44. {
  45.     SFReplyRec2     myReply;
  46.  
  47.     zapLocals();
  48.  
  49.     myReply.nameRefDesc = myReply.pathRefDesc = refIsNewHandle;
  50.     SFGetFile2(                 /* Do an incredibly ordinary SFGetFile2. */
  51.         dx, dy,
  52.         refIsPointer,
  53.         promptStr,
  54.         filterProc,
  55.         typeList,
  56.         &myReply
  57.     );
  58.  
  59.     if (myReply.good) {         /* User did pick something... */
  60.  
  61.         if (file->pathRef) DisposeHandle(file->pathRef);
  62.         if (file->nameRef) DisposeHandle(file->nameRef);
  63.             /* Dispose the old SFGetFile2 ref handles, since we */
  64.             /* have new ones to play with.                      */
  65.  
  66.         file->pathRef = (char **)myReply.pathRef;
  67.         file->nameRef = (char **)myReply.nameRef;
  68.             /* Record the new ones. */
  69.  
  70.     }
  71.     else {      /* User cancelled, so we don't want these handles. */
  72.         DisposeHandle(myReply.pathRef);
  73.         DisposeHandle(myReply.nameRef);
  74.     }
  75. }
  76.  
  77. /**********************************************************************/
  78.  
  79. void            spreadTheWord(file, theRefNum)
  80. FileIO          *file;
  81. unsigned int    theRefNum;
  82. {
  83.     /* We want the file refNum in all the structures that may be used. */
  84.  
  85.     file->close.refNum     =
  86.     file->newline.refNum   =
  87.     file->readWrite.refNum =
  88.     file->getPos.refNum    =
  89.     file->setPos.refNum    =
  90.         theRefNum;
  91. }
  92.  
  93. /**********************************************************************/
  94.  
  95. unsigned int    openFile(file)
  96. FileIO          *file;
  97. {
  98.     if (!file->pathRef) return(_fileErr = 0xFFFF);
  99.         /* We can't open what we don't have. */
  100.  
  101.     HLock(file->pathRef);                   /* Lock down the file name. */
  102.     file->open.pathname = (GSString255 *)(*file->pathRef + 2);
  103.                                             /* Class 1 str. from SFGetFile2. */
  104.  
  105.     OpenGS(&file->open);                    /* Open the file. */
  106.     _fileErr = _toolErr;
  107.     HUnlock(file->pathRef);
  108.     if (_fileErr) return(_fileErr);
  109.  
  110.     spreadTheWord(file, file->open.refNum);
  111.  
  112.     NewlineGS(&file->newline);              /* Set how lines are to be read. */
  113.     if (_fileErr = _toolErr) {
  114.         CloseGS(&file->close);
  115.         spreadTheWord(file, 0);
  116.         return(_fileErr);                   /* We had a problem. */
  117.     }
  118.  
  119.     return(0);                              /* _fileErr = 0 also. */
  120. }
  121.  
  122. /**********************************************************************/
  123.  
  124. unsigned int    readFile(file)
  125. FileIO          *file;
  126. {
  127.     ReadGS(&file->readWrite);
  128.     _fileErr = _toolErr;
  129.     line[file->readWrite.transferCount] = '\0';
  130.     return(file->readWrite.transferCount);
  131. }
  132.  
  133. /**********************************************************************/
  134.  
  135. unsigned int    closeFile(file)
  136. FileIO          *file;
  137. {
  138.     /* Note that this routines does not dispose of the SFGetFile2
  139.     ** ref handles.  Just because the file is closed doesn't mean
  140.     ** that the application is done with the file name.  It is up
  141.     ** to the application to dispose of these handles if they are
  142.     ** no longer needed or wanted.  After disposing of the handles,
  143.     ** it is important to set them to NULL so that other routines
  144.     ** know that there isn't a current file name.
  145.     ** Lister never worries about getting rid of them.  If a new
  146.     ** file is selected, getFileName() takes care of the old ones.
  147.     ** Also, when the application is quit, the current file
  148.     ** ref handles are disposed by quitting the application, so
  149.     ** lister has all the cases covered.
  150.     */
  151.  
  152.     CloseGS(&file->close);
  153.     if (_fileErr = _toolErr) return(_fileErr);
  154.     spreadTheWord(file, 0);
  155.  
  156.     return(0);
  157. }
  158.  
  159. /**********************************************************************/
  160.  
  161. unsigned int    fileErr()
  162. {
  163.     if (_fileErr) ErrorWindow(0, NULL, _fileErr);
  164.     return(_fileErr);
  165. }
  166.